/*******************************************************************
 * 外语翻译平台(translate) 
 *
 * 成都一亿信息技术公司 版权所有
 * 
 * Copyright (C) 2010 Transer Information Technology Co., Ltd.
 *
 * @Version : 1.0  2011-3-1 下午09:51:24
 *
 * OfferProjectAction.java 
 *
 *******************************************************************/
package com.transer.web.action.project;

import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;
import org.apache.struts2.ServletActionContext;

import com.transer.persistance.bean.Project;
import com.transer.persistance.bean.ProjectOfferInfo;
import com.transer.persistance.dao.IProjectDao;
import com.transer.persistance.dao.IProjectOfferInfoDao;
import com.transer.utils.DateUtils;
import com.transer.web.action.TranserActionSupport;

/**
 * class defination are there
 * 
 * @author Mark
 * 
 * @version 1.0 ywengineer@gmail.com
 * 
 */
public class OfferProjectAction extends TranserActionSupport {

	private static final long serialVersionUID = 6630415591067833064L;
	// 日志
	private static final Logger logger = Logger.getLogger(OfferProjectAction.class);
	// 信息
	private ProjectOfferInfo info;
	// 竞标操作
	private IProjectOfferInfoDao projectOfferDao;
	// 项目操作
	private IProjectDao projectDao;
	// 上一页面
	private String referer;
	// 竞标结果
	private String offerResult;

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.opensymphony.xwork2.ActionSupport#execute()
	 */
	@Override
	public String execute() throws Exception {
		try {
			// 查看是否存在此项目
			Project project = projectDao.getProjectInfo(info.getProjectID());
			// 如果不存在项目,返回失败
			if (project == null) {
				offerResult = TranserActionSupport.NONE;
			} else {
				// 如果是自己发布的项目,不能对进行竞标
				if (project.getUserID() == user.getId()) {
					offerResult = TranserActionSupport.LOGIN;
				} else {
					// 查看此项目是否在竞标期内
					if (DateUtils.isBetween(project.getStart(), project.getEnd())) {// 在
						// 查看是否已经竞标过此项目
						ProjectOfferInfo exsit = projectOfferDao.getOfferInfo(info.getProjectID(), user.getId());
						// 如果没有,允许竞标
						if (exsit == null) {
							// 设置用户 ID
							info.setUser(user.getId());
							// 添加竞标信息
							projectOfferDao.addOfferInfo(info);
							// 竞标成功
							offerResult = TranserActionSupport.SUCCESS;
						} else {
							// 已竞标过此项目
							offerResult = TranserActionSupport.NONE;
						}
					} else {// 不在
						// 竞标结束了
						offerResult = TranserActionSupport.INPUT;
					}
				}
			}
		} catch (Exception e) {
			logger.error("竞标出错" + e.getMessage());
			// 竞标出错
			offerResult = TranserActionSupport.ERROR;
		}
		// 返回上一页面
		referer = ServletActionContext.getRequest().getHeader("referer");
		// 如果存在参数,取消参数
		if (StringUtils.isNotEmpty(referer) && StringUtils.contains(referer, "?")) {
			referer = StringUtils.substring(referer, 0, StringUtils.indexOf(referer, "?"));
		}
		// 返回
		return TranserActionSupport.SUCCESS;
	}

	/**
	 * @return the info
	 */
	public ProjectOfferInfo getInfo() {
		return info;
	}

	/**
	 * @param info
	 *            the info to set
	 */
	public void setInfo(ProjectOfferInfo info) {
		this.info = info;
	}

	/**
	 * @return the projectOfferDao
	 */
	public IProjectOfferInfoDao getProjectOfferDao() {
		return projectOfferDao;
	}

	/**
	 * @param projectOfferDao
	 *            the projectOfferDao to set
	 */
	public void setProjectOfferDao(IProjectOfferInfoDao projectOfferDao) {
		this.projectOfferDao = projectOfferDao;
	}

	/**
	 * @return the referer
	 */
	public String getReferer() {
		return referer;
	}

	/**
	 * @param referer
	 *            the referer to set
	 */
	public void setReferer(String referer) {
		this.referer = referer;
	}

	/**
	 * @return the offerResult
	 */
	public String getOfferResult() {
		return offerResult;
	}

	/**
	 * @param offerResult
	 *            the offerResult to set
	 */
	public void setOfferResult(String offerResult) {
		this.offerResult = offerResult;
	}

	/**
	 * @return the projectDao
	 */
	public IProjectDao getProjectDao() {
		return projectDao;
	}

	/**
	 * @param projectDao
	 *            the projectDao to set
	 */
	public void setProjectDao(IProjectDao projectDao) {
		this.projectDao = projectDao;
	}
}